package com.laundry.controller;

import com.laundry.dto.ResultDTO;
import com.laundry.entity.News;
import com.laundry.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/news")
public class NewsController {
    
    @Autowired
    private NewsService newsService;
    
    @GetMapping("/list")
    public ResultDTO getNews() {
        try {
            return ResultDTO.success(newsService.findAll());
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDTO.error("获取新闻列表失败");
        }
    }
    
    @GetMapping("/detail")
    public ResultDTO getNewsById(@RequestParam Long id) {
        try {
            return ResultDTO.success(newsService.findById(id));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDTO.error("获取新闻详情失败");
        }
    }
    
    @PostMapping("/create")
    public ResultDTO createNews(@RequestBody News news) {
        try {
            return ResultDTO.success(newsService.save(news));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDTO.error("创建新闻失败");
        }
    }
    
    @PutMapping("/update")
    public ResultDTO updateNews(@RequestBody News news) {
        try {
            return ResultDTO.success(newsService.update(news));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDTO.error("更新新闻失败");
        }
    }
    
    @DeleteMapping("/delete")
    public ResultDTO deleteNews(@RequestParam Long id) {
        try {
            newsService.deleteById(id);
            return ResultDTO.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDTO.error("删除新闻失败");
        }
    }
} 